package com.zxh.listdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.zxh.listdemo.adapter.list.MultiAdapter;
import com.zxh.listdemo.adapter.list.MultiBaseAdapter;
import com.zxh.listdemo.adapter.list.OneAdapter;
import com.zxh.listdemo.adapter.list.OneBaseAdapter;
import com.zxh.listdemo.adapter.list.common.BaseMultiModel;
import com.zxh.listdemo.listener.PicssoScrollListener;
import com.zxh.listdemo.model.OneModel;
import com.zxh.listdemo.model.TwoModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 公共的列表页
 *
 * @author zhangxuehui
 */
public class ListActivity extends AppCompatActivity {

    public static String LIST_TYPE;
    //网络图片合集
    private String[] mImgLink = {
            "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1519733759156&di=f1862d01c8aceed13612999df5edac9c&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2Fe1fe9925bc315c600dce09d386b1cb13495477b6.jpg",
            "http://h.hiphotos.baidu.com/image/h%3D300/sign=836eeeae16950a7b6a3548c43ad0625c/c8ea15ce36d3d5397966ba5b3187e950342ab0cb.jpg",
            "http://a.hiphotos.baidu.com/image/pic/item/d6ca7bcb0a46f21f46612acbfd246b600d33aed5.jpg",
            "http://g.hiphotos.baidu.com/image/pic/item/c8ea15ce36d3d539f09733493187e950342ab095.jpg",
            "http://c.hiphotos.baidu.com/image/pic/item/91529822720e0cf3457ad8150146f21fbf09aa4b.jpg",
            "http://a.hiphotos.baidu.com/image/pic/item/8694a4c27d1ed21bc6aa1b32a66eddc450da3f81.jpg",
            "http://d.hiphotos.baidu.com/image/pic/item/9922720e0cf3d7ca600211eff91fbe096a63a9b6.jpg",
            "http://g.hiphotos.baidu.com/image/pic/item/42a98226cffc1e17cce4097a4190f603738de926.jpg",
            "http://h.hiphotos.baidu.com/image/pic/item/060828381f30e924d7da088847086e061d95f709.jpg",
            "http://h.hiphotos.baidu.com/image/pic/item/908fa0ec08fa513d5971fe2a366d55fbb3fbd96f.jpg"
    };
    private ListView mListView;
    private int mType;//设置显示类型
    private List<OneModel> mOnes = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        getIntentData();
        initView();
        initAdapter();
    }

    private void initAdapter() {
        if (mType == R.id.tv_listview_one) {
            mListView.setAdapter(new OneAdapter(getApplicationContext(), getOneListData()));
        } else if (mType == R.id.tv_listview_one_encap) {
            mOnes.addAll(getOneListData());
            mListView.setAdapter(new OneBaseAdapter(mOnes, R.layout.item_one));
            mListView.setOnScrollListener(new PicssoScrollListener(this));
        } else if (mType == R.id.tv_listview_multi) {
            mListView.setAdapter(new MultiAdapter(getMultiListData(), this));
//            mListView.setOnScrollListener(new PicssoScrollListener(this));
        } else if (mType == R.id.tv_listview_multi_encap) {
            mListView.setAdapter(new MultiBaseAdapter(getMultiListData()));
            mListView.setOnScrollListener(new PicssoScrollListener(this));
        }
    }

    private void initView() {
        mListView = findViewById(R.id.lv_content);
    }

    /**
     * 虚拟数据
     *
     * @return
     */
    private List<OneModel> getOneListData() {
        List<OneModel> list = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            int pos = mOnes.size() + i;
            list.add(new OneModel("第" + pos + "条信息", "这里没啥可说的", mImgLink[pos % mImgLink.length]));
        }
        return list;
    }

    /**
     * 虚拟数据
     *
     * @return
     */
    private List<BaseMultiModel> getMultiListData() {
        List<BaseMultiModel> list = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            int pos = list.size() + i;
            if (i % 2 == 0) {
                list.add(new TwoModel("文字布局---->第" + pos + "条信息").setItemType(Config.ITEM_TEXT));
            } else {
                list.add(new OneModel("第" + pos + "条信息", "这里没啥可说的", mImgLink[pos % mImgLink.length]).setItemType(Config.ITEM_IMG));
            }
        }
        return list;
    }


    public void getIntentData() {
        mType = getIntent().getIntExtra(MainActivity.SHOW_VIEW_TYPE, R.id.tv_listview_one);
    }

    private BaseAdapter getAdapter() {
        return null;
    }


}
